<script setup>
import { Edit, Delete ,Search} from "@element-plus/icons-vue";

import {  noticeListService ,noticeAddService,noticeUpdateService,noticeDeleteService ,noticeBatchDeleteService,expNoticeServer,NoticeUploadService} from "@/api/notice.js";
import { courseListService } from "@/api/course.js";
import { ref } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";



//用户搜索时的关键字
const input = ref("");
const content = ref("");
//列表数据模型
const notices = ref([]);

//分页条数据模型
const pageNum = ref(1); //当前页
const total = ref(20); //总条数
const pageSize = ref(20); //每页条数

//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
  pageSize.value = size;
  getNoticeList();
};
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
  pageNum.value = num;
  getNoticeList();
};
//回显题型
const getQuestuinTypeList = async () => {
  let result = await noticeTypeListService();
  categorys.value = result.data;
};

//获取公告列表数据
const getNoticeList = async () => {
  //设置查询参数
  let params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
    content: content.value?content.value:null,
    name: input.value?input.value:null
  };
  let result = await noticeListService(params);
  //刷新数据
  notices.value = result.data.items;
  total.value = result.data.total;
};
getNoticeList();

//控制添加还是修改
const isAdd = ref(true);
//控制抽屉是否显示
const visibleDrawer = ref(false)
//添加表单数据模型
const dataModel = ref({
  name: "",
  content:""
})
//发送创建表单数据
const noticeAdd = async () => {
  let result = await noticeAddService(dataModel.value);
  ElMessage.success(result.msg ? result.msg : "添加成功");
  //刷新列表
  visibleDrawer.value = false;
  //清除表单数据
  dataModel.value = {};
  getNoticeList();
  
};
//发送修改表单数据
const noticeUpdate = async () => {
  let result = await noticeUpdateService(dataModel.value);
  ElMessage.success(result.msg ? result.msg : "修改成功");
  //刷新列表
  visibleDrawer.value = false;
  //清除表单数据
  dataModel.value = {};
  getNoticeList();
};
//展示添加表单
const showAddDialog = () => {
  visibleDrawer.value = true;
  dataModel.value = {};
}
//展示修改表单
const showUpdateDialog = (row) => {
  isAdd.value = false;
  visibleDrawer.value = true;
  dataModel.value = row;
};
//发送删除公告请求
const noticeDelete = (row) => {
  ElMessageBox.confirm("确定要删除吗？", "提示", {
    type: "warning",
  })
    .then(async () => {
      let result = await noticeDeleteService(row.id);
      ElMessage.success(result.msg ? result.msg : "删除成功");
      getNoticeList();
    })
    .catch(() => {});
}
const multipleSelection = ref([]);// 多选数据
const handleSelectionChange = (val) => {
  multipleSelection.value = val;
};


const delBatch = async () => {
  if (multipleSelection.value.length === 0) {
    ElMessage.warning("请选择要删除的数据");
    return;
  }

  ElMessageBox.confirm("确定要删除吗？", "提示", {
    type: "warning",
  })
    .then(async () => {
      let result = await noticeBatchDeleteService(multipleSelection.value);
      ElMessage.success(result.msg ? result.msg : "删除成功");
      getNoticeList();
    })
    .catch(() => {});
};


// 文件输入框引用
const dialogVisible = ref(false);
const inputFile = ref();

// 触发文件选择对话框
const handleImportClick = () => {
  dialogVisible.value = true;
};

// 处理文件选择
const handleFileChange = (file, fileList) => {
  if (fileList.length > 1) {
    fileList.shift(); // 移除第一个文件，保留最新的文件
  }
  inputFile.value = fileList;
};

// 上传文件
const uploadFile = async () => {
  if (inputFile.value.length === 0) {
    ElMessage.warning("请选择一个文件");
    return;
  }

  const file = inputFile.value[0].raw;
  try {
    let result = await NoticeUploadService(file);
    ElMessage.success(result.msg ? result.msg : "上传成功");
    dialogVisible.value = false;
    getNoticeList();
  } catch (error) {
    ElMessage.error("上传失败: " + error.msg);
  }
};

//导出报表函数
const exp = async () => {
  let result = await expNoticeServer();
  ElMessage.success(result.msg ? result.msg : "下载成功");
}

</script>                                       
<template>
  <el-card class="page-container transparent-card">
    <template #header>
      <div class="header">
        
        <div class="extra">
          <el-button type="primary" @click="showAddDialog()">添加公告</el-button>
          <el-button type="danger" @click="delBatch()">批量删除</el-button>
          <el-button type="success" @click="exp()">导出报表</el-button>
          <el-button type="primary" @click="handleImportClick">导入excel</el-button> 
        </div>
      </div>
    </template>
    <!-- 搜索表单 -->
    <el-form inline>
      <el-form-item>
        <el-input
          v-model="input"
          style="width: 240px"
          placeholder="请输入公告标题"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="content"
          style="width: 240px"
          placeholder="请输入公告内容"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getNoticeList">搜索</el-button>
        <el-button @click="input='';content=''">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 文章列表 -->
    <el-table :data="notices" style="width: 100%" @selection-change="handleSelectionChange" ref="table" :row-key="row => row.id">
      <el-table-column  width="100" type="selection" :reserve-selection="true"> </el-table-column>
      <el-table-column label="公告标题" width="400" prop="name"></el-table-column>
      <el-table-column label="公告内容" width="400" prop="content"></el-table-column>
      <el-table-column label="公告时间" width="400" prop="time"></el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="{ row }" class="button-container">
          <el-button :icon="Edit" circle plain type="primary" @click="showUpdateDialog(row)"></el-button>
          <el-button :icon="Delete" circle plain type="danger" @click="noticeDelete(row)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <!-- 分页条 -->

<!-- 抽屉 -->
<el-drawer
      v-model="visibleDrawer"
      title="添加公告"
      direction="rtl"
      size="50%"
    >
      <el-form :model="dataModel" label-width="100px">
        <el-form-item label="公告标题">
          <el-input
            v-model="dataModel.name"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item label="公告内容">
          <el-input
            v-model="dataModel.content"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="isAdd ? noticeAdd() : noticeUpdate()"

          >
          {{isAdd ? '添加' : '修改'}}
          </el-button>
          <el-button type="info" @click="visibleDrawer = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>

  <!-- 导入对话框 -->
  <el-dialog v-model="dialogVisible" title="导入Excel文件" width="30%">
      <el-upload
        ref="upload"
        action=""
        :auto-upload="false"
        :on-change="handleFileChange"
        :file-list="inputFile"
        accept=".xlsx, .xls"
        :show-file-list="true"
      >
        <template #trigger>
          <el-button type="primary">选取文件</el-button>
        </template>
        <el-button type="success" @click="uploadFile()">上传文件</el-button>
      </el-upload>
    </el-dialog>


    <el-pagination
      v-model:current-page="pageNum"
      v-model:page-size="pageSize"
      :page-sizes="[3, 5, 10, 15]"
      layout="jumper, total, sizes, prev, pager, next"
      background
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
  </el-card>
</template>
<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
.transparent-card {
  background-color: rgba(255, 255, 255, 0.8); /* 半透明背景 */
  backdrop-filter: blur(10px); /* 模糊效果 */
  border-radius: 8px; /* 圆角 */
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); /* 可选：轻微阴影效果 */
}
.el-select {
  width: 200px;
}
.button-container {
  display: flex;
  gap: 8px;
}

/* 抽屉样式 */
.avatar-uploader {
    :deep() {
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .el-upload {
            border: 1px dashed var(--el-border-color);
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            transition: var(--el-transition-duration-fast);
        }

        .el-upload:hover {
            border-color: var(--el-color-primary);
        }

        .el-icon.avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            text-align: center;
        }
    }
}
.editor {
  width: 100%;
  :deep(.ql-editor) {
    min-height: 200px;
  }
}
</style>
